<?php

namespace app\home\controller;

use think\Controller;
use think\Db;
use think\Loader;
use think\Request;

class commodity extends Controller
{
    /**
     * 显示资源列表
     */
    public function lists()
    {
        $sg_ccolumn = Loader::model('commodity')->columnInfo();
        $this->assign('columnInfo',$sg_ccolumn);
        $cid = input('get.cid');
        $a = Loader::model('commodity')->allInfo($cid);
        foreach ($a as &$v){
            $v['image'] = explode('|',$v['image'])[1];
        }
        $this->assign('co_Info',$a);
        return view();
    }
    public function buy(){
        $se_commodity = Loader::model('commodity')->Sellings();
        $sg_ccolumn = Loader::model('commodity')->columnInfo();
        foreach ($se_commodity as &$vv){
            $vv['image'] = explode('|',$vv['image'])[1];
        }
        $this->assign('columnInfo',$sg_ccolumn);
        $uid = session('UserXinxi')['user']['uid'];
        $id = input('get.id');
        $a = Loader::model('commodity')->coInfo($id);
        $v = explode('|',$a['image']);
        $a['image'] = $v[0];
        $a['image2'] = $v[1];
        $a['image3'] = $v[2];
        $a['image4'] = $v[3];
        $this->assign('info',$se_commodity);
        $this->assign('coInfo',$a);
        return view();
    }
    public function cart(){
        if (Request::instance()->isGet()){
            $uid = session('UserXinxi')['user']['uid'];
            $b = Loader::model('commodity')->cartInfo($uid);
            foreach ($b as &$v){
                $v['image'] = explode('|',$v['image'])[1];
            }

            $this->assign('catrInfo',$b);
            return view();
        }
        if (Request::instance()->isPost()){
            $coid = input('post.coid');
            $num = input('post.num');
            $uid = session('UserXinxi')['user']['uid'];
            $a = Loader::model('commodity')->cartAdd($coid,$num,$uid);
            if ($a){
                return json(['msg'=>'添加成功','status'=>'1']);
            }
            return json(['msg'=>'添加失败','status'=>'-1']);
        }
    }
    public function cartDel(){
        $id = input('post.id','');
        $a = Loader::model('commodity')->cartDel($id);
        if ($a){
            return json(['msg'=>'删除成功','status'=>1]);
        }
        return json(['msg'=>'删除失败','status'=>-1]);
    }

    public function order(){
        $sumAll = '';
        $uid = session('UserXinxi')['user']['uid'];
        if (Request::instance()->isGet()){
            $coid = input('get.coid');
            $num = input('get.num');
            $b[] = Loader::model('commodity')->coInfo($coid);
            $b[0]['num'] = $num;
            $sumAll = $b[0]['price'] * $num;
        }else{
            $b = Loader::model('commodity')->cartInfo($uid);
            foreach ($b as $v){
                $sumAll += $v['sum'];
            }
            $isca = input('post.isca');
            $this->assign('isca',$isca);
        }
        $this->assign('sumAll',$sumAll);
        $siteInfo = Loader::model('user')->siteInfo($uid);
        $this->assign('siteInfo',$siteInfo);
        $this->assign('coInfo',$b);
        return view();
    }

    public function makeOrder(){
        $uid = session('UserXinxi')['user']['uid'];
        $num = input('post.num/a');
        $coId = input('post.coid/a');
        $seid = $coId[0];
        if (empty($coId[0])){
            return json(['status'=>-1,'msg'=>'请选择收货地址']);
        }
        $a = array_slice($coId,1);//把收货地址id从数组中剔除,只剩下商品id数组
        //循环读出商品信息

        $b = [];
        foreach ($a as $v){
            $b[] = Loader::model('commodity')->coInfoOr($v);//名称 id 单价
        }
        $number  = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
        $sumAll='';
        $gg = [];
        foreach ($b as $k=>$v){
            $sum = $v['price'] * $num[$k];
            $sumAll += $sum;
            $gg[] = $v + ['sum'=>$sum,'coid'=>$a[$k],'num'=>intval($num[$k])];

        }
        $orInfo =[
            'number'=>$number,
            'uid'=>$uid,
            'total'=>$sumAll,
        ];
        $d = Loader::model('commodity')->orderAdd($orInfo,$seid,$uid,$gg,$num);
        if ($d ==-1){
            return json(['status'=>-1,'msg'=>'添加失败']);
        }
        return json(['status'=>1,'msg'=>'添加成功']);
    }

    public function orderLook(){
        $sg_ccolumn = Loader::model('commodity')->columnInfo();
        $this->assign('columnInfo',$sg_ccolumn);
        $uid = session('UserXinxi')['user']['uid'];
        $dfk = Loader::model('commodity')->dfkInfo(1,$uid);
        $dfkDingdan = Loader::model('commodity')->ddInfo(1,$uid);
        $dfhDingdan = Loader::model('commodity')->ddInfo(2,$uid);
        $dshDingdan = Loader::model('commodity')->ddInfo(3,$uid);
        $dpjDingdan = Loader::model('commodity')->ddInfo(4,$uid);
        $this->assign('dfkDingdan',$dfkDingdan);
        $this->assign('dfhDingdan',$dfhDingdan);
        $this->assign('dshDingdan',$dshDingdan);
        $this->assign('dpjDingdan',$dpjDingdan);
        return view();
    }

    public function cartNum(){
        $uid = session('UserXinxi')['user']['uid'];
        $a = Loader::model('commodity')->cartNum($uid);
        return json($a);
    }

    public function orderInfo(){
        $orid = input('get.id');
        $orindex = input('get.orindex');
        $a = Loader::model('commodity')->orderInfo($orid);
        foreach ($a as &$v){
            $v['image'] = explode('|',$v['image'])[1];
        }
        $this->assign('orderInfo',$a);
        $this->assign('orindex',$orindex);
        return view();
    }

    public function comment(){
        $orid = input('get.orid');
        $a = Loader::model('commodity')->orderInfo($orid);
        $this->assign('coInfo',$a);
        $this->assign('orid',$orid);
        return view();
    }

    public function commentAdd(){
        $orid = input('post.orid');
        $uid = session('UserXinxi')['user']['uid'];
        $coid = input('post.coid/a');
        $comment = input('post.comment/a');
        $commentInfo = [];
        foreach ($coid as $k=>$v){
            $commentInfo[] = ['coid'=>$v,'comment'=>$comment[$k],'uid'=>$uid];
        }
        $a = Loader::model('commodity')->commentAdd($commentInfo,$orid);
        if ($a['status']< 0){
            return json(['status'=>-1,'msg'=>$a['msg']]);
        }
        return json(['status'=>1,'msg'=>$a['msg']]);
    }

}
